<style lang="less">
</style>
<template>
  <div>
    <h2>总积分 {{ groupIntegral }} </h2>
    <Table
      border highlight-row
      :columns="columns" :data="tableData"></Table>
    <Modal
      v-model="showModifyDialog"
      title="修改积分"
      @on-ok="ok">
      <Input v-model="currentItem.integral" placeholder="请输入修改后的积分"></Input>
    </Modal>
  </div>
</template>
<script>
import { getUserList, updateUser } from '@/api/users'
import { getGroupList, updateGroup } from '@/api/group'

export default {
  components: {
  },
  data () {
    return {
      原总积分: 0,
      groupIntegral: 0,
      showModifyDialog: false,
      currentItem: {},
      registerNum: '',
      tableData: [],
      columns: [{
        type: 'index',
        width: 60,
        align: 'center'
      }, {
        title: '姓名',
        key: 'name'
      }, {
        title: '积分',
        key: 'integral'
      }, {
        title: 'Action',
        key: 'action',
        width: 150,
        align: 'center',
        render: (h, params) => {
          return h('div', [
            h('Button', {
              props: {
                type: 'primary',
                size: 'small'
              },
              style: {
                marginRight: '5px'
              },
              on: {
                click: () => {
                  this.showModifyDialog = true
                  this.currentItem = params.row
                }
              }
            }, '修改积分')
          ])
        }
      }]
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getGroupIntegral () {
      let param = { id: localStorage.groupid }
      getGroupList(param).then(({data}) => {
        this.groupIntegral = data.integral
        this.原总积分 = this.groupIntegral
        this.tableData.forEach(item => {
          this.原总积分 += item.integral
        })
      })
    },
    updateGroupIntegral () {
      let resultIntegral = this.原总积分
      console.log(resultIntegral)
      this.tableData.forEach(item => {
        console.log(item)
        resultIntegral -= item.integral
      })

      console.log(resultIntegral)

      let param = {
        integral: resultIntegral
      }
      updateGroup(localStorage.groupid, param).then(data => {
        this.getGroupIntegral()
      })
    },
    getList () {
      let param = {
        groupid: localStorage.groupid, isChecked: 1
      }
      getUserList(param).then(res => {
        this.tableData = res.data.rows
        this.getGroupIntegral()
      })
    },
    ok () {
      let param = { integral: this.currentItem.integral }

      updateUser(this.currentItem.id, param).then(res => {
        let param = {
          groupid: localStorage.groupid, isChecked: 1
        }
        getUserList(param).then(res => {
          this.tableData = res.data.rows
          this.updateGroupIntegral()
        })
      })
    },
    btnAddClick () {
      let res = []
      for (var i = 0; i < this.registerNum; i++) {
        let code = (new Date()).valueOf()
        // eslint-disable-next-line no-unused-vars
        let param = {
          groupid: 1, code
        }
        addCode(param).then(res => {
          console.log(res)
        })
        res.push({code})
        this.tableData = res
      }
    }
  }
}

</script>
<style>
</style>
